import {Controller, Get, Inject} from '@nestjs/common';
import {ApiTags} from "@nestjs/swagger";
import {InjectModel} from "nestjs-typegoose";
import {Category} from "@libs/db/models/category.model";
import {Model} from "mongoose";
import {Crud} from "nestjs-mongoose-crud";

@Controller('categories')
@Crud({
  model: Category
})
@ApiTags('分类')
export class CategoriesController {
  constructor(@InjectModel(Category) private readonly model: Model<Category>
  ) {
  }

  @Get('options')
  async getOptions() {
    const categories = (await this.model.find()).map(cate => ({
      label: cate.name,
      value: cate._id
    }))
    return {
      title: '分类管理',
      columns: [
        {label: 'ID', prop: '_id'},
        {label: '分类名称', prop: 'name', search: true},
        {label: '上级分类', prop: 'parent', select: true, selectData: categories}
      ],
    }
  }
}
